define(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {

    var table = layui.table,
        treetable = layui.treetable,
        iconPickerFa = layui.iconPickerFa,
        colorpicker = layui.colorpicker,
        laydate = layui.laydate,
        autocomplete = layui.autocomplete;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.setconfig/index',
        add_url: 'system.setconfig/add',
        delete_url: 'system.setconfig/delete',
        edit_url: 'system.setconfig/edit',
        modify_url: 'system.setconfig/modify',
        export_url: 'system.setconfig/export',
    };
    var Controller = {
        index: function () {

            ea.table.render({
                init: init,
                limits:[15,20,30,40,50,60,70,80,90,100,500,1000,5000,10000],

                toolbar:['refresh',[
                    // {
                    //     text: '生成页面',
                    //     url: 'system.setconfig/createjs',
                    //     method: 'open',
                    //     auth: '',
                    //     class: 'layui-btn layui-btn-success layui-btn-sm',
                    //     icon: 'fa fa-hourglass',
                    //     extend: 'data-table="' + init.table_render_id + '"',
                    // },
                ],'add','delete'
                ],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID',search:false},
                    {field: 'group', title: '分组'},
                    {field: 'remark', title: '备注'},
                    {field: 'sort', title: '排序'},
                    {field: 'name', title: '字段name'},
                    {field: 'value', title: '值',search:false},
                    {field: 'formtype', title: '表单类型',search:false,selectList:{"input":"单行文本","textarea":"多行文本","file":"单文件","files":"多文件","img":"单图片","imgs":"多图片","video":"视频","radio":"单选框","checkbox":"多选框","select":"下拉框","color":"颜色","editor":"编辑框","none":"隐藏框","password":"密码框","optgroup":"下拉组","switch":"开关","json":"json数组","lradio":"联动单选框","lcheckbox":"联动多选框","lselect":"联动下拉框","datetime":"时间"}},
                    {field: 'create_time', title: '创建时间',search:false},
                    // {field: 'status', title: '状态',  search: false, selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            'edit',
                            [
                            ],
                            'delete'
                        ]
                    }
                ]],
            });

            // renderTable();

            $('body').on('click', '[data-treetable-refresh]', function () {
                renderTable();
            });

            $('body').on('click', '[data-treetable-delete]', function () {
                var tableId = $(this).attr('data-treetable-delete'),
                    url = $(this).attr('data-url');
                tableId = tableId || init.table_render_id;
                url = url != undefined ? ea.url(url) : window.location.href;
                var checkStatus = table.checkStatus(tableId),
                    data = checkStatus.data;
                if (data.length <= 0) {
                    ea.msg.error('请勾选需要删除的数据');
                    return false;
                }
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                });
                ea.msg.confirm('确定删除？', function () {
                    ea.request.post({
                        url: url,
                        data: {
                            id: ids
                        },
                    }, function (res) {
                        ea.msg.success(res.msg, function () {
                            renderTable();
                        });
                    });
                });
                return false;
            });

            ea.table.listenSwitch({filter: 'status', url: init.modify_url});

            ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

            ea.listen();
        },
        add: function () {
            $(".icon").each(function (i,v) {
                var ids = $(v).attr('id');
                var myiconPickerFa = iconPickerFa.render({
                    elem:"#"+ids,
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#'+ids).val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                })
            })
            $(".color").each(function (i,v) {
                var ids = $(v).attr('id');
                var coles = $('[name='+ids+']').val();
                //初始色值
                var mycolorpicker = colorpicker.render({
                    elem: '#'+ids
                    ,color: coles //设置默认色
                    ,change:function (color) {
                        $('[name='+ids+']').val(color);
                    }
                });
            })
            $(".datetime").each(function (i,v) {
                var ids = $(v).attr('id');
                laydate.render({
                    elem: '#'+ids
                    ,type: 'datetime'
                });
            })
            ea.listen(function (data) {
                console.log(data)
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        edit: function () {
            $(".icon").each(function (i,v) {
                var ids = $(v).attr('id');
                var myiconPickerFa = iconPickerFa.render({
                    elem:"#"+ids,
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#'+ids).val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                })
            })
            $(".color").each(function (i,v) {
                var ids = $(v).attr('id');
                var coles = $('[name='+ids+']').val();
                //初始色值
                var mycolorpicker = colorpicker.render({
                    elem: '#'+ids
                    ,color: coles //设置默认色
                    ,change:function (color) {
                        $('[name='+ids+']').val(color);
                    }
                });
            })
            $(".datetime").each(function (i,v) {
                var ids = $(v).attr('id');
                laydate.render({
                    elem: '#'+ids
                    ,type: 'datetime'
                });
            })
            ea.listen(function (data) {
                console.log(data)
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        }
    };
    return Controller;
});
